class MyThread extends Thread{

    @Override
    public void run() {
        for(int i=0;i<5;i++){
            System.out.println("（继承Thread类）运行线程"+Thread.currentThread().getName()+", 输出i的值:"+i);
            
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

class MyRunnable implements Runnable{

    @Override
    public void run() {
        
        for(int i=0;i<5;i++){
            System.out.println("（实现Runnable接口）运行线程"+Thread.currentThread().getName()+", 输出i的值:"+i);
            
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
}
public class ThreadTest {
    public static void main(String[] args){
        MyThread th1=new MyThread();
        th1.setName("子线程1");
        th1.start();
        
        MyThread th2=new MyThread();
        th2.setName("子线程2");
        th2.start();
        System.out.println("主线程运行完毕");
        
        MyRunnable run1=new MyRunnable();
        Thread th3=new Thread(run1);
        th3.start();
        
        MyRunnable run2=new MyRunnable();
        Thread th4=new Thread(run2);
        th4.start();
    }
}
